package com.lord.rowcopy.controller;

import java.io.File;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lord.rowcopy.excel.BusinessSheet;
import com.lord.rowcopy.excel.ExcelUtil;
import com.lord.rowcopy.service.RowcopyService;

/**
 * @Description: excel行复制控制器
 * @author Lord
 * @date 2022年01月19日
 */
@RestController
@RequestMapping("/rowcopy")
public class RowcopyController {

    /**
     * 临时路径，存放上传或下载临时产生的一些文件，一般从application.yml配置文件中获取，
     * 这里为了方便演示，直接写死了，这个文件目录需要先创建，不然会报找不到模板的异常
     */
    public String tmpPath = "D:\\rowcopy\\tmp";

    @Resource
    private RowcopyService rowcopyService;

    /**
     * 相关报表-生意贷表（导出Excel）
     * 访问地址：http://127.0.0.1:8080/rowcopy/exportBusinessLoan
     * 
     * @return
     */
    @GetMapping(value = "/exportBusinessLoan")
    public ResponseEntity<String> exportBusinessLoan(HttpServletRequest request, HttpServletResponse response) {

        // 获取模拟数据，这里为了演示，并未使用数据库
        Map<String, Object> map = rowcopyService.getApprovalBusinessSheetData();

        try {
            String fileName = "贷款审批表——生意贷.xls";
            File exportFile = BusinessSheet.getInstance().exportFile(map, "business.xls", tmpPath, fileName);
            ExcelUtil.getInstance().setBrowser(response, exportFile, fileName);
            return new ResponseEntity<>("导出成功", HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("导出失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}
